﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADO.NET.Samples.Common;
using ADO.NET.Samples.DBML;

namespace ADO.NET.Samples.Forms.LINQ.LINQToSQL
{
    [FormDescription("LINQ to SQL : Modify, AddNew and Delete", "Demonstrate how to use LINQ to modify, delete and add new data from SQL Server Database.")]
    public partial class LINQtoSQL_ModifyAddNewDelete : Form
    {
        ArticleDataClassesDataContext dataContext = new ArticleDataClassesDataContext();

        public LINQtoSQL_ModifyAddNewDelete()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Data binding
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LINQtoSQL_ModifyAddNew_Load(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// Bind data
        /// </summary>
        private void BindData()
        {
            this.userBindingSource.DataSource = dataContext.Users;
        }

        /// <summary>
        /// Set data to textboxes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.listBox1.SelectedValue != null)
            {
                this.textBoxUserID.Text = this.listBox1.SelectedValue.ToString();
                this.textBoxUserName.Text = this.listBox1.Text;
            }
        }

        /// <summary>
        /// Add new item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBoxUserName.Text))
            {
                ADO.NET.Samples.DBML.User user = new DBML.User { UserName = this.textBoxUserName.Text };
                dataContext.Users.InsertOnSubmit(user);
                dataContext.SubmitChanges();
                dataContext = new ArticleDataClassesDataContext();
                BindData();
                MessageBox.Show("Add succeeds!");
            }
        }

        /// <summary>
        /// Update current selected item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBoxUserName.Text))
            {
                var user = (from u in dataContext.Users
                            where u.UserID == Convert.ToInt32(this.textBoxUserID.Text)
                            select u).Single();
                user.UserName = this.textBoxUserName.Text;
                dataContext.SubmitChanges();
                this.listBox1.SelectedValue = this.textBoxUserID.Text;
                MessageBox.Show("Update succeeds!");
            }
        }

        /// <summary>
        /// delete selected item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.textBoxUserName.Text))
            {
                var user = (from u in dataContext.Users
                            where u.UserID == Convert.ToInt32(this.listBox1.SelectedValue)
                            select u).Single();
                dataContext.Users.DeleteOnSubmit(user);
                dataContext.SubmitChanges();
                dataContext = new ArticleDataClassesDataContext();
                BindData();
                MessageBox.Show("Delete succeeds!");
            }
        }
    }
}
